/*  main.c
 *
 *  Copyright (C) 2008 Guillaume Béland <guillaume.beland@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 *
 *  Author : Guillaume Béland <guillaume.beland@gmail.com>
*/ 

#include <xmmsclient/xmmsclient.h>
#include <xmmsclient/xmmsclient-glib.h>

#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <glib/gprintf.h>
#include <string.h>
#include <time.h>
#include <getopt.h>
#include <unistd.h>

#include "audioscrobbler.h"

static void     parse_arg (xmms_client_data_t *clientdata, int argc, char **argv);
static void     audioscrobbler_quit (G_GNUC_UNUSED void *data) ;

/* Main */
int
main (int argc, char **argv) {
        
        xmms_client_data_t *clientdata;
        clientdata = g_new (xmms_client_data_t, 1);

        clientdata->connection = xmmsc_init ("XMMS2-AUDIOSCROBBLER");
       
	if (!clientdata->connection) {
		fprintf (stderr, "OOM!\n");
		exit (EXIT_FAILURE); /* Connection to xmms2 failed badly */
	}

	if (!xmmsc_connect (clientdata->connection, getenv ("XMMS_PATH"))) {
		fprintf (stderr, "[INFO] Connection failed: %s\n", 
		         xmmsc_get_last_error (clientdata->connection));

		exit (EXIT_FAILURE); /* Connection failed */
	}

	printf ("\n[INFO] Client initialized"); 

        clientdata->mainloop = g_main_loop_new (NULL, FALSE);
        clientdata->audioscrobbler = xmms_audioscrobbler_init ();

        parse_arg (clientdata, argc, argv);
    
        /* when a song is played, create an entry, we pass clientdata struct
         * which contain the connection and the audioscrobbler object
         */
        XMMS_CALLBACK_SET (clientdata->connection, xmmsc_broadcast_playback_current_id,
                           xmms_audioscrobbler_handle_current_id, clientdata);
        XMMS_CALLBACK_SET (clientdata->connection, xmmsc_broadcast_playback_status,
                           xmms_audioscrobbler_handle_playback_status, clientdata);
	XMMS_CALLBACK_SET (clientdata->connection, xmmsc_signal_playback_playtime,
	                   xmms_audioscrobbler_handle_playtime, NULL);
    
        xmmsc_disconnect_callback_set (clientdata->connection, audioscrobbler_quit, NULL);    

        /* Handshake */
        xmms_audioscrobbler_submit_data (clientdata->audioscrobbler, 2);
	xmmsc_mainloop_gmain_init (clientdata->connection);
	g_main_loop_run (clientdata->mainloop);

        return EXIT_SUCCESS;
}

/* Parse argument passed through command line. username and password for the Last.fm account */
static void
parse_arg (xmms_client_data_t *clientdata, int argc, char **argv) {

        int opt;
	char *text;
        gchar *opt_username_arg = NULL;
        gchar *opt_password_arg = NULL;
        gboolean opt_debug = FALSE;
        gchar *config_file;
        gchar *path;
        
	struct option long_options[] = {
		{"username",  required_argument, NULL, 'u'},
		{"password",  required_argument, NULL, 'p'},
                {"debug", no_argument, NULL, 'd'},
		{"help",  no_argument, NULL, 'h'},
		{0, 0, 0, 0}
	};

        while ( (opt = getopt_long (argc, argv, "c:dhmnl::v", long_options, NULL)) != -1) {
	        switch (opt) {
		        case 'u':	/* username */
			        opt_username_arg = g_strdup (optarg);
			        break;
		        case 'p':	/* password */
			        opt_password_arg = g_strdup (optarg);
			        break;
		        case 'd':	/* debug */
			        opt_debug = TRUE;
			        break;
		        case 'h':
		        case '?':
		        default:  /* Default, show the help*/
                                text = g_strdup_printf ("\nUsage: xmms2-audioscrobbler [options...] [arguments]\n\n"
                                " Help Options:\n"
                                "   -h --help            Show this help \n\n"
                                "   -u --username        Set the username \n"
                                "   -d --debug           Print debugging information \n"
                                "   -p --password        Set the password \n\n");
			        g_printf (text);
				g_free (text);
				exit (EXIT_SUCCESS);
			break;
		}
	}

        path = xmms_audioscrobbler_get_config_dir ();
        config_file = g_strconcat (path, CONFIG_FILE, NULL);

	clientdata->audioscrobbler->debug = opt_debug;
               
        if (opt_username_arg != NULL)
                clientdata->audioscrobbler->username = g_strdup (opt_username_arg);
        if (opt_password_arg != NULL)
                clientdata->audioscrobbler->password = g_strdup (opt_password_arg);

	if ( (opt_username_arg != NULL || opt_password_arg != NULL) && xmms_audioscrobbler_check_config_file (path)) {
	        xmms_audioscrobbler_write_config (clientdata->audioscrobbler, config_file);
        } else
	        xmms_audioscrobbler_read_config (clientdata->audioscrobbler, config_file);
	        
        g_free (path);
        g_free (config_file);
        g_free (opt_username_arg);
        g_free (opt_password_arg);

}

/**
 * Quit callback function, called when xmms2d is killed
 */
static void
audioscrobbler_quit (G_GNUC_UNUSED void *data) {
	exit (EXIT_SUCCESS);
}

